////////////////////////////////////////////////////////////////////////////////
//  Copyright 2011 Adam Kidd
//  
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//  
//    http://www.apache.org/licenses/LICENSE-2.0
//  
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
////////////////////////////////////////////////////////////////////////////////

package com.arcadefoundry.rest.http
{
	import flash.utils.ByteArray;

	public class HTTPRequest implements IHTTPRequest
	{
		//------------------------------------------------
		//
		// Constructor
		//
		//------------------------------------------------
		/**
		 * The <code>HTTPRequest</code> class is used to form an HTTP Request
		 * for sending via the HTTPClient.
		 * 
		 * @param method The HTTPRequestMethod to be performed on the resource.
		 * @param uri The URI of the resource.
		 * 
		 */
		public function HTTPRequest(method:String, uri:String)
		{
			this.method = method;
			this.uri = uri;
		}
		
		//------------------------------------------------
		//
		// Methods
		//
		//------------------------------------------------
		/**
		 *  
		 * @return A String representation of the request for debugging comms.
		 * 
		 */
		public function toString():String
		{
			var output:String = method + " " + uri + "\n"
								+ headers.toString + "\n"
								+ body;
			return output;
		}
		
		//------------------------------------------------
		//
		// Getters & Setters
		//
		//------------------------------------------------
		/**
		 * The HTTP Headers to be sent with the request. 
		 */
		private var _headers:IHTTPRequestHeaders = new HTTPRequestHeaders();
		public function get headers():IHTTPRequestHeaders
		{
			return _headers;
		}
		
		public function set headers(value:IHTTPRequestHeaders):void
		{
			_headers = value
		}
		
		/**
		 * The body of the request. Only used in PUT & POST requests. 
		 */
		private var _body:String;
		public function get body():String
		{
			return _body;
		}
		
		public function set body(value:String):void
		{
			_body = value;
			if (body)
			{
				setContentLength();
			}
			else
			{
				headers.removeHeader("Content-Length");
			}
		}
		
		/**
		 * Sets the content length header when the request body is modified. 
		 * 
		 */
		private function setContentLength():void
		{
			// Get the byte length of the string to set the content length.
			var bodyBytes:ByteArray = new ByteArray();
			bodyBytes.writeUTFBytes(_body);
			bodyBytes.position = 0;
			// Set the content length
			headers.setContentLength(bodyBytes.bytesAvailable);
			// Cleanup
			bodyBytes = null;
		}
		
		// Default to GET to prevent accidental modification of data.
		/**
		 * The HTTPRequestMethod to be performed on the resource. 
		 */
		private var _method:String = HTTPRequestMethod.GET;
		public function get method():String
		{
			return _method;
		}
		
		public function set method(value:String):void
		{
			// Only allow setting supported methods.
			if (HTTPRequestMethod.isValidMethod(value))
			{
				_method = value;
			}
		}
		
		/**
		 * The uri of the resource.  
		 */
		private var _uri:String;
		public function get uri():String
		{
			return _uri;
		}
		
		public function set uri(value:String):void
		{
			_uri = value;
		}
	}
}
